﻿using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Office.OneNote.Interop;

namespace Microsoft.Office.OneNote
{
    [DebuggerDisplay("Section {Name}")]
    [OneNoteXmlElementAttribute("Section")]
    [OneNoteHierarchyType(CreateHierarchyType.Section)]
    [SuppressMessage("Microsoft.Maintainability", "CA1501:AvoidExcessiveInheritance")]
    public sealed class OneNoteSection : OneNoteSectionBase
    {
        private const string FileExtension = ".one";

        internal static readonly OneNoteProperty ColorProperty = OneNoteProperty.Register("color", typeof(OneNoteColor), typeof(OneNoteSection), OneNoteColor.Automatic);

        internal OneNoteSection(OneNoteObjectId id)
            : base(id)
        {
        }

        public override OneNoteColor Color
        {
            get { return GetValue<OneNoteColor>(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        public OneNotePage CreatePage()
        {
            OneNoteObjectId pageId = OneNoteApplication.Current.CreateNewPage(ObjectId);
            Update();

            return (OneNotePage)Storage[pageId];
        }

        internal static OneNoteSection Open(string name, OneNoteFolder parent, bool openExisting)
        {
            Contract.RequiresValidFileName(name, "name");

            if (!name.EndsWith(FileExtension, StringComparison.OrdinalIgnoreCase))
            {
                name += FileExtension;
            }

            return parent.Storage.Open<OneNoteSection>(name, parent.ObjectId, openExisting);
        }
    }
}
